import java.io.*;
import java.net.*;
import static java.lang.Integer.*;



/**
 * Created by zyl on 17-6-1.
 */
public class Sever1 {
    public static void main(String args[]) {

        MyDC receiver = new MyDC();

        try {
            ServerSocket server = null;
            try {
                server = new ServerSocket(3500);
                //鍒涘缓涓??涓猄erverSocket鍦ㄧ鍙??3500鐩戝惉瀹㈡埛璇锋眰

            } catch (Exception e) {
                System.out.println("can not listen to:" + e);
                //鍑洪敊锛屾墦å鍑洪敊淇℃伅

            }
            Socket socket = null;
            try {
                socket = server.accept();
                //浣跨敤accept()闃诲绛夊緟瀹㈡埛璇锋眰锛屾湁瀹㈡埛

                //璇锋眰鍒°鏉ュ垯浜х敓涓??涓猄ocket瀵硅薄锛屽苟缁х画鎵§琛??

            } catch (Exception e) {
                System.out.println("Error." + e);
                //鍑洪敊锛屾墦鍗板嚭閿欎俊鎭??

            }

            String line;

            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //鐢盨ocket瀵硅薄寰楀埌杈撳叆娴侊紝骞舵瀯閫犵浉搴旂殑BufferedReader瀵硅薄

            PrintWriter os = new PrintWriter(socket.getOutputStream());
            //鐢±Socket瀵硅薄寰楀埌杈撳嚭娴侊紝骞舵瀯閫燩rintWriter瀵硅薄

            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            //鐢辩郴缁熸爣鍑嗚緭鍏ヨ澶囨瀯閫燘ufferedReader瀵硅薄

            System.out.println("Client:" + is.readLine());
            //鍦ㄦ爣鍑嗚緭鍑轰笂鎵撳嵃浠庡鎴风璇诲叆鐨勫瓧绗︿覆


            line = sin.readLine();
            //浠庢爣鍑嗚緭鍏ヨ鍏ヤ竴瀛楃涓??


            while (!line.equals("bye")) {
                //濡傛灉璇ュ瓧绗︿覆涓?? "bye"ï鍒欏仠姝㈠惊鐜??

                os.println(receiver.evaluate(line));
                //鍚戝鎴风杈撳鸿瀛楃涓??

                os.flush();
                //鍒锋柊杈撳嚭娴侊紝浣緾lient椹笂鏀跺埌è瀛楃涓??

                System.out.println("Server:" + receiver.evaluate(line));
                //鍦ㄧ郴缁熸爣鍑嗚緭鍑轰笂鎵撳嵃璇诲叆鐨勫瓧绗︿覆

                System.out.println("Client:" + is.readLine());
                //浠嶤lient璇诲叆涓??瀛楃涓诧紝骞¶鎵撳嵃鍒版爣鍑嗚緭鍑轰笂

                line = sin.readLine();
                //浠庣郴缁熸爣鍑嗚緭å璇诲叆涓??瀛楃涓??

            }  //缁х画寰ª鐜??

            os.close(); //鍏抽棴Socket杈撳嚭娴??

            is.close(); //鍏抽棴Socket杈撳叆娴??

            socket.close(); //鍏抽棴Socket

            server.close(); //鍏抽棴ServerSocket

        } catch (Exception e) {
            System.out.println("Error:" + e);
//鍑洪敊锛屾墦鍗板嚭閿欎俊鎭??

        }
    }

}

